package net.ge.mosq.web.others;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ge.mosq.jdo.entity.UserInfo;

/**
 * @author noodles.wumu@gmail.com
 * @since 2012/07/02 16:19:17
 * @version 1.0
 */
public class URIDispatcher extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1623295153082486180L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// TEST CODE -------------------------------------------
		if (req.getSession().getAttribute("userInfo") == null) {
			UserInfo ui = new UserInfo();
			ui.setEmail("noodles.wumu@gmail.com");
			ui.setEmail("common");
			req.getSession().setAttribute("userInfo", ui);
		}
		// TEST CODE -------------------------------------------

		Map<String, String> urlMap = (Map<String, String>) this
				.getServletContext().getAttribute("DispatchPatterns");
		if (urlMap.containsKey(req.getAttribute("requestUri"))) {
			req.getRequestDispatcher(urlMap.get(req.getAttribute("requestUri")))
					.forward(req, resp);
		} else {
			req.getRequestDispatcher((String) req.getAttribute("requestUri"))
					.forward(req, resp);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

}
